package com.hwhhhh.graduationproject.service.impl;

import com.hwhhhh.graduationproject.dao.entity.AppointmentEntity;
import com.hwhhhh.graduationproject.dao.entity.FreeClassroomEntity;
import com.hwhhhh.graduationproject.dao.repo.AppointmentRepo;
import com.hwhhhh.graduationproject.dao.repo.FreeClassroomRepo;
import com.hwhhhh.graduationproject.domain.model.FreeClassroom;
import com.hwhhhh.graduationproject.exception.NoFreeClassroomException;
import com.hwhhhh.graduationproject.service.ClassroomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description detail
 * Created by Hwhhhh on 2021/3/2 19:02
 */
@Service
public class ClassroomServiceImpl implements ClassroomService {
    @Autowired
    private FreeClassroomRepo freeClassroomRepo;
    @Autowired
    private AppointmentRepo appointmentRepo;

    @Override
    public List<FreeClassroom> getFreeClassroomsByFreeType(int freeType) throws NoFreeClassroomException {
        List<FreeClassroomEntity> entityList = this.freeClassroomRepo.getByFreeTypeOrderByFreeTypeAscAppointNumAsc(freeType);
        if (entityList.isEmpty()) throw new NoFreeClassroomException("没有空闲教室！");
        // 使用stream()获得所需要的List<FreeClassroom>
        return entityList.stream().map(entity -> {
            FreeClassroom freeClassroom = new FreeClassroom();
            BeanUtils.copyProperties(entity, freeClassroom);
            return freeClassroom;
        }).collect(Collectors.toList());
    }

    /**
     * 根据教室编号查询教室
     * @param key
     * @return
     */
    @Override
    public Map<Integer, String> findByClassroomNumLike(String key) throws NoFreeClassroomException{
        List<FreeClassroomEntity> entityList = this.freeClassroomRepo.findIdAndAndClassroomNumByClassroomNumLike("%" + key + "%");
        if (entityList.isEmpty()) throw new NoFreeClassroomException("没有相近编号的空闲教室");
        // stream List 转 Map
        return entityList.stream().collect(Collectors.toMap(FreeClassroomEntity::getId, FreeClassroomEntity::getClassroomNum));
    }

    @Override
    public FreeClassroom getById(int id) {
        FreeClassroomEntity entity = this.freeClassroomRepo.getById(id);
        FreeClassroom freeClassroom = new FreeClassroom();
        BeanUtils.copyProperties(entity, freeClassroom);
        return freeClassroom;
    }

    @Override
    public void updateOccupyNum(int id, int op) {
        FreeClassroomEntity entity = this.freeClassroomRepo.getById(id);
        if ( op == 1 ) entity.setOccupyNum(entity.getOccupyNum() + 1);
        else entity.setOccupyNum(entity.getOccupyNum() - 1);
        this.freeClassroomRepo.save(entity);
    }

    @Override
    public void makeAppointment(int freeClassroomId, int uid) {
        FreeClassroomEntity freeClassroomEntity = this.freeClassroomRepo.getById(freeClassroomId);
        AppointmentEntity appointmentEntity = new AppointmentEntity();
        appointmentEntity.setAppointDate(freeClassroomEntity.getFreeDate());
        appointmentEntity.setClassroomNum(freeClassroomEntity.getClassroomNum());
        appointmentEntity.setFreeClassroomId(freeClassroomId);
        appointmentEntity.setFreeType(freeClassroomEntity.getFreeType());
        appointmentEntity.setUid(uid);
        this.appointmentRepo.save(appointmentEntity);
    }

    @Override
    public void cancelAppointment(int id) {
        AppointmentEntity appointmentEntity = this.appointmentRepo.getOne(id);
        appointmentEntity.setIsEffective(0);
    }
}
